/**
 * @Created by zhuly8 zhuly8@yusys.com.cn on 2019-8-5 09:59:16.
 * @updated by
 * @description 应用系统管理
 */
define(
	function (require, exports) {
		/**
		 * 页面加载完成时触发
		 * @param hashCode 路由ID
		 * @param data 传递数据对象
		 * @param cite 页面站点信息
		 */
		exports.ready = function (hashCode, data, cite) {
			yufp.lookup.reg('SCRIPT_READER');//解析规则
			yufp.custom.vue({
				el: cite.el,
				data: function () {
					/*定义校验规则，只能填字母数字 下划线*/
						var valiCodes = function (rule, value, callback) {
							var reg = /^[0-9a-zA-Z_]{1,}$/;
							if (value && reg.test(value)) {
								if(value.length > 64){
									callback(new Error('长度不能大于64位'));
								}else{
									callback();
								}						
							} else if (value && !reg.test(value)) {
								callback(new Error('能填字母、数字和下划线'));
							} else {
								callback();
							}
						};
					return {
						tbHeight:'100%',
						rulesone:[
							{validator: valiCodes, trigger: 'blur'  }
						],
						searchFormdata: {},
						addFnButton: !yufp.session.checkCtrl('addFn'),
						updateFnButton: !yufp.session.checkCtrl('updateFn'),
						deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
						dataUrl: 'api/use/citiccard/jobtype/queryJobTypeList',
						saveBtnShow: true,
						cancelBtnShow: true,
						formdata: {},
						isReadOnly: false,
						saveDisabled: false,
						async: false,
						height: yufp.frame.size().height,
						dialogVisible: false,
						viewTitleArrry: ['解析规则新增', '解析规则修改'],
						viewTitle: '解析规则新增',
						addFlag: true,
						needSrcEnvParam: true,
						needTarEnvParam: true,
						scriptFileParamX:false,//脚本文件位置是否必输
						srcSchameParamX:false,//源表数库位置
						srcTabParamX:false,//源表位置
						tarSchameParamX:false,//目标数据库为止
						tarTabParamX:false,//目标表位置
						confFileX:false,//map文件位置
						
					};
				},
				methods: {
					changeRequire:function(item){ //根据解析规则修改输入框是否必输
						if('CMD' == item){ //“源表环境位置，源表数库位置：源表位置：目标环境位置：目标数据库位置：目标表位置” 为必须输入项
							this.scriptFileParamX = false;
							this.srcSchameParamX = false;
							this.srcTabParamX = false;
							this.tarSchameParamX = false;
							this.tarTabParamX = false;
							this.confFileX = false;
							this.needSrcEnvParam = false;
							this.needTarEnvParam = false;
							
							
						}else if('HIVE' == item){//hive解析器 “脚本文件位置：”，“Map文件路径：”，“源表环境位置：”，目标环境位置
							this.needSrcEnvParam = true;
							this.needTarEnvParam = true;
							this.scriptFileParamX = true;
							this.srcSchameParamX = false;
							this.srcTabParamX = false;
							this.tarSchameParamX = false;
							this.tarTabParamX = false;
							this.confFileX = true;
						}else if('SCRIPT' == item){//脚本解析器  "脚本文件位置：","源表环境位置：","目标环境位置：",Map文件路径
							this.needSrcEnvParam = true;
							this.needTarEnvParam = true;
							this.scriptFileParamX = true;
							this.srcSchameParamX = false;
							this.srcTabParamX = false;
							this.tarSchameParamX = false;
							this.tarTabParamX = false;
							this.confFileX = true;
						}else{
							return;
						}
					},
					/**
					 * 取消
					 */
					cancelFn: function () {
						var _this = this;
						_this.clearAllFn();
						_this.dialogVisible = false;
					},

					/** 校验环境信息  */
					checkEnvIsNull: function(envLocation,schemaLocation,tableLocation ){

						return (envLocation==null||
							envLocation==='')||
							(schemaLocation==null||
								schemaLocation==='')
							|| 	(tableLocation==null||
								tableLocation==='');

						},

					/**
					 * 保存
					 */
					saveFn: function () {
						var _this = this;
						var validate = false;
						_this.$refs.refForm.validate(function (valid) {
							validate = valid;
						});
						if (!validate) {
							return;
						}

						//当操作为命令行的时候，源表环境位置和目标环境位置不能同时为看
						if(_this.$refs.refForm.formdata.scriptReader==='CMD'){

							if(_this.checkEnvIsNull(_this.$refs.refForm.formdata.srcEnvParam,
								_this.$refs.refForm.formdata.srcSchameParam,
								_this.$refs.refForm.formdata.srcTabParam,
								)&& _this.checkEnvIsNull(_this.$refs.refForm.formdata.tarEnvParam,
								_this.$refs.refForm.formdata.tarSchameParam,
								_this.$refs.refForm.formdata.tarTabParam)

							) {
								_this.$message({
										message: "源表环境相关信息 与 目标环境相关信息 不能同时为空",
										type: "warning"
								});
								return;
							}
						}

						var paramObj = _this.$refs.refForm.formdata;
						let url = 'api/use/citiccard/jobtype/saveJobType';
						if(!_this.addFlag){
							url = 'api/use/citiccard/jobtype/updateJobType';
						}
						yufp.service.request({
							method: 'POST',
							url: url,
							data: paramObj,
							callback: function (code, message, response) {
								if (response.code != 0) {
									_this.$message(response.message);
								} else {
									_this.$refs.refTable.remoteData();
									_this.$message('操作成功');
									_this.dialogVisible = false;
								}
							}
						});

					},
					/**
					 * 控制保存按钮、xdialog、表单的状态
					 * @param viewType 表单类型
					 * @param editable 可编辑,默认false
					 */
					switchStatus: function (viewType, editable) {
						var _this = this;
						_this.viewType = viewType;
						_this.dialogVisible = true;
						_this.saveBtnShow = editable;
						_this.cancelBtnShow = editable;
					},
					/**
					 * 新增
					 */
					addFn: function () {
						var _this = this;
						_this.viewTitle = _this.viewTitleArrry[0];
						_this.dialogVisible = true;
						_this.addFlag = true;
						//清空form
						_this.$nextTick(function(){
							_this.clearAllFn();
							
							
						})
					},

					clearAllFn: function () {
						var _this = this;
						_this.$refs.refForm.resetFields();
					},

					/**
					 * 修改
					 */
					modifyFn: function () {
						var _this = this;
						if (_this.$refs.refTable.selections.length != 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning' });
							return;
						}
						_this.viewTitle = _this.viewTitleArrry[1];
						_this.dialogVisible = true;
						_this.addFlag = false;
						_this.$nextTick(function () {
							_this.formdata.jobTypeCode = _this.$refs.refTable.selections[0].jobTypeCode;
							_this.formdata.jobTypeName = _this.$refs.refTable.selections[0].jobTypeName;
							_this.formdata.scriptReader = _this.$refs.refTable.selections[0].scriptReader;
							_this.formdata.scriptFileParam = _this.$refs.refTable.selections[0].scriptFileParam;
							_this.formdata.srcTabParam = _this.$refs.refTable.selections[0].srcTabParam;
							_this.formdata.tarEnvParam = _this.$refs.refTable.selections[0].tarEnvParam;
							_this.formdata.tarTabParam = _this.$refs.refTable.selections[0].tarTabParam;
							_this.formdata.confFileSup = _this.$refs.refTable.selections[0].confFileSup;
							_this.formdata.confFileCont = _this.$refs.refTable.selections[0].confFileCont;							
							_this.formdata.confFile = _this.$refs.refTable.selections[0].confFile;
							_this.formdata.srcEnvParam = _this.$refs.refTable.selections[0].srcEnvParam;
							_this.formdata.srcSchameParam = _this.$refs.refTable.selections[0].srcSchameParam;
							_this.formdata.srcTabSuffix = _this.$refs.refTable.selections[0].srcTabSuffix;
							_this.formdata.tarSchameParam = _this.$refs.refTable.selections[0].tarSchameParam;
							_this.formdata.tarTabSuffix = _this.$refs.refTable.selections[0].tarTabSuffix;
																												
						})
					},
					/**
					 * 删除
					 */
					deleteFN: function () {
						var _this = this;
						var selections = _this.$refs.refTable.selections;
						if (selections.length != 1) {
							_this.$message({ message: '请先选择一条记录', type: 'warning' });
							return;
						}
						_this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
							confirmButtonText: '确定',
							cancelButtonText: '取消',
							type: 'warning',
							center: true,
							callback: function (action) {
								if (action === 'confirm') {
									yufp.service.request({
										method: 'DELETE',
										url: 'api/use/citiccard/jobtype/deleteJobType',
										data: _this.$refs.refTable.selections[0],
										callback: function (code, message, response) {
											if (response.code == 0) {
												_this.$refs.refTable.remoteData();
												_this.$message('操作成功');
											} else {
												_this.$message(response.message);
											}
										}
									});
								}
							}
						});
					},


				},
				mounted: function () {
						var _this = this;
						_this.$nextTick(function(){
								var tableBox = _this.$refs.tableBox;
								let a = tableBox.getBoundingClientRect().top + 28;
								var theight = `calc(100vh - ${a}px - 64px)`;
								_this.tbHeight = theight;		
						});
				}
			});
		};
	});